<?php
/**
 * Created by PhpStorm.
 * todo: app端版本控制
 * Date: 2016/10/9
 * Time: 15:55
 */

namespace Admin\Controller;
use Common\Controller\AdminbaseController;
use Admin\Controller\Tencentyun\Cosapi;

class VersionController extends AdminbaseController
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 版本列表
     */
    public function index(){
        $version = M("version");
        $list    = $version->where(array('status'=>1))->order('system_type asc')->select();
        $this->assign('list', $list);
        $this->display();
    }
    
    /**
     * 添加版本
     */
    public function create(){
        $id = I("id");
        $version = M("version");
        $version_info = $version->where(array('id'=>$id))->find();
        $this->assign('version_info', $version_info);
        $this->assign('id', $id);
        $this->display();
    }

    /**
     * 版本入库
     */
    public function add_version_bak(){
        $id = I("id");
        $version = I("version");
        $cname   = I("url");
     	$info    = I('post.info');
     	$update_mode = I('post.mode');
     	$system_type = I('post.system_type');

        $versionObj = M("version");
        $data['version'] = $version;
        $data['url']     = $cname;
        $data['status']  = 1;
        $data['system_type'] = $system_type;   //0:android  1:ios
        $data['info']    = $info;
        $data['update_mode'] = $update_mode;   //0：不用必须更新   1：必须更新   
        $data['create_time'] = time();
        
        $data['user_id']  = $_SESSION['ADMIN_ID'];
        if(!empty($id)){
            $version_info = $versionObj->where(array('id'=>$id))->find();
            if(!empty($version_info)){
                unlink($version_info['url']);
                $versionObj->where(array('id'=>$id))->save($data);
            }
        }else{
            $data['create_time'] = time();
            if($versionObj->add($data)){
            	
            }
        }

        $this->success('添加成功！', U("Admin/Version/index"));
    }
    
    /**
     * 新版本入库
     * 增加历史
     */
    public function add_version(){
    	$id = I("id");
    	$version = I("version");
    	$cname   = I("url");
    	$info    = I('post.info');
    	$update_mode = I('post.mode');
    	$system_type = I('system_type');
        $formal      = I('formal');
        $is_update   = I('is_update');
        $show_video  = I('show_video');
    	$versionObj = M("version");
    	$data['version'] = $version;
    	$data['url']     = $cname;
    	$data['status']  = 1;
    	$data['system_type'] = $system_type;   //0:android  1:ios
    	$data['info']    = $info;
    	$data['update_mode'] = $update_mode;   //0：不用必须更新   1：必须更新
    	$data['create_time'] = time();
    	$data['user_id']  = $_SESSION['ADMIN_ID'];
    	$data['create_time'] = time();
		$data['status'] = 1;
        $data['is_formal'] = $formal;  //兑吧控制 0审核，1线上
        $data['is_update'] = $is_update;  //是否提示更新 0否，1是
        $data['show_video'] = $show_video;  //是否显示视频栏目 0否，1是

    	//检查版本是否修改
    	$currnet_version = $versionObj->where(array('id'=>$id))->field('version,status')->find();
//     	print_r($currnet_version);die;
    	if($currnet_version['version'] == $version){
    		//修改最新版本
    		$update_data = array(
				'info' 		  => $data['info'],
    			'url'  		  => $data['url'],
    			'update_mode' => $data['update_mode'],
    			'is_formal'      => $data['is_formal'],
                'is_update'      => $data['is_update'],
                'show_video'     => $data['show_video']
    		);
//     		print_r($update_data);die;
    		$versionObj->where(array('id'=>$id))->save($update_data);
    		$this->success('修改成功！', U("Admin/Version/index"));
    		exit();
    	}

        if($currnet_version['status'] == 1) {
            if ($versionObj->add($data)) {
                $versionObj->where(array('id' => $id))->save(array('status' => 0));
            }
            $this->success('添加成功！', U("Admin/Version/index"));
        }
    }
    
    /**
     * 查看历史版本
     */
    public function historyVersion(){
    	$page_size = '10';
    	$sys_type  = I('system_type');
    	$count = M('version')->where(array('system_type'=>$sys_type, 'status'=>0))->count();
    	$page = $this->page($count, $page_size);
    	$list = M('version')->where(array('system_type'=>$sys_type, 'status'=>0))->order('create_time desc')->limit($page->firstRow, $page->listRows)->select();
    	$this->assign('list', $list);
		$this->assign('page', $page->show('Admin'));
		$this->display('history');
    }
    
    
    /**
     * 删除
     */
    public function delete(){
        $id = I("id");
        $versionObj = M("version");
        $version_info = $versionObj->where(array('id'=>$id))->find();
        if(!$version_info){
            return $this->error("信息不存在");
        }
        $data['status'] = 2;
        $result = $versionObj->where(array('id'=>$id))->save($data);
        unlink($version_info['url']);
        if ($result) {
            $this->success("删除成功！");
        } else {
            return $this->error($result['Message']);
        }
    }
}